package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.PmsProductLadder;
import com.itheima.domain.PmsSkuStock;
import com.itheima.mapper.PmsSkuStockMapper;
import com.itheima.service.db.PmsSkuStockService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsSkuStockServiceImpl implements PmsSkuStockService {

    @Autowired
    PmsSkuStockMapper pmsSkuStockMapper;
    @Override
    public List<PmsSkuStock> getByIdSku(Integer pid, String keyword) {
        LambdaQueryWrapper<PmsSkuStock> lqw=new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,pid)
                .like(StrUtil.isNotBlank(keyword),PmsSkuStock::getSkuCode,keyword);

        return pmsSkuStockMapper.selectList(lqw);
    }

    @Override
    public void updatePid(Integer pid, PmsSkuStock[] skuStockList) {
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStock.setProductId(pid.longValue());
            pmsSkuStockMapper.updateById(pmsSkuStock);
        }
    }

    @Override
    public List<PmsSkuStock> selectByProductId(Integer id) {
        LambdaQueryWrapper<PmsSkuStock> lqw=new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,id);
        return pmsSkuStockMapper.selectList(lqw);
    }

    @Override
    public void updateByProductId(Integer id, List<PmsSkuStock> PmsSkuStockList) {
        LambdaQueryWrapper<PmsSkuStock> lqw=new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,id);
        pmsSkuStockMapper.delete(lqw);

        for (PmsSkuStock PmsSkuStock : PmsSkuStockList) {
            PmsSkuStock.setProductId(id.longValue());
            pmsSkuStockMapper.insert(PmsSkuStock);
        }

    }

    @Override
    public void save(Long productId, List<PmsSkuStock> skuStockList) {
        for (PmsSkuStock PmsSkuStock : skuStockList) {
            PmsSkuStock.setProductId(productId);
            pmsSkuStockMapper.insert(PmsSkuStock);
        }
    }
}
